﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using System.IO;
using YuuzleComic.DBUtility;
using System.Net;
using System.Text;

public partial class yzadmin_data_comicdata : YuuzleComic.Web.UI.ManagePage
{
    protected string json = "";
    private JObject result = new JObject();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            switch (Request["a"])
            {
                case "list":
                    this.ProcessList();
                    break;
                case "tree":
                    this.ProcessTree();
                    break;
                case "drop":
                    this.ProcessDrop();
                    break;
                case "delete":
                    this.ProcessDelete();
                    break;
                default:
                    break;
            }
        }
    }

    private void ProcessDelete()
    {
        try
        {
            string[] pathArr = Request["path"].Split('|');
            foreach (string path in pathArr)
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                else if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }
            }
            result.Add("success", true);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message);
        }
        finally
        {
            json = result.ToString();
        }
    }

    private void ProcessDrop()
    {
        try
        {
            DirectoryInfo sdir = new DirectoryInfo(Request["s"]);
            this.CopyDirectoryAndFiles(Request["d"], sdir);
            if (sdir.GetDirectories().Length == 0)
            {
                sdir.Delete(true);
            }
            string stpadd = Request["s"].Replace("\\", "/");
            string dtpadd = (Request["d"] + "\\" + sdir.Name).Replace("\\", "/");
            foreach (KeyValuePair<string, string> item in YuuzleComic.Config.verConfig.comicDatas)
            {
                stpadd = stpadd.Replace(item.Value, "");
                dtpadd = dtpadd.Replace(item.Value, "");
            }
            DbHelperSQL.ExecuteSql("update zj set tpadd='" + dtpadd + "/' where tpadd='" + stpadd + "/'");
            DataSet ds = DbHelperSQL.Query("select id from zj where tpadd='" + dtpadd + "/'");
            List<string> idlist = new List<string>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                idlist.Add(dr["id"].ToString());
            }
            result.Add("success", true);
            result.Add("zjid", string.Join(",", idlist.ToArray()));
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message);
        }
        finally
        {
            json = result.ToString();
        }
    }

    private void ProcessTree()
    {
        JArray data = new JArray();
        try
        {
            DirectoryInfo pdir = new DirectoryInfo(Request["path"]);
            foreach (FileSystemInfo fsi in pdir.GetFileSystemInfos())
            {
                if (fsi.Attributes != FileAttributes.Hidden && fsi.Attributes != FileAttributes.System)
                {
                    JObject jo = new JObject();
                    string path = fsi.FullName.Replace("\\", "/");
                    foreach (KeyValuePair<string, string> item in YuuzleComic.Config.verConfig.comicDatas)
                    {
                        path = path.Replace(item.Value, "");
                    }
                    bool islink = DbHelperSQL.Exists("select count(*) from zj where tpadd='" + path + "/'");
                    jo.Add("text", fsi.Name);
                    jo.Add("path", fsi.FullName);
                    if (fsi is DirectoryInfo)
                    {
                        jo.Add("icon", islink ? "extjs/icons/folder_link.png" : "extjs/icons/folder.png");
                    }
                    else
                    {
                        jo.Add("icon", "extjs/icons/picture.png");
                    }
                    jo.Add("leaf", fsi is DirectoryInfo && ((DirectoryInfo)fsi).GetFileSystemInfos().Length == 0 || fsi is FileInfo);
                    jo.Add("allowDrag", islink);
                    jo.Add("checked", false);
                    data.Add(jo);
                }
            }
        }
        catch (Exception e)
        {
            throw;
        }
        finally
        {
            json = data.ToString();
        }
    }

    private void ProcessList()
    {
        try
        {
            JArray data = new JArray();
            foreach (KeyValuePair<string, string> item in YuuzleComic.Config.verConfig.comicDatas)
            {
                JObject jo = new JObject();
                jo.Add("name", item.Key);
                jo.Add("value", item.Value);
                data.Add(jo);
            }
            result.Add("data", data);
            result.Add("success", true);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message);
        }
        finally
        {
            json = result.ToString();
        }
    }

    //复制里面的文件夹和文件
    private void CopyDirectoryAndFiles(string des, DirectoryInfo srcDir)
    {
        if (!des.EndsWith("\\"))
        {
            des += "\\";
        }
        string desPath = des + srcDir.Name + "\\";
        if (!Directory.Exists(desPath))
        {
            Directory.CreateDirectory(desPath);
        }

        foreach (FileInfo file in srcDir.GetFiles())
        {
            file.CopyTo(desPath + file.Name, true);
        }
        foreach (DirectoryInfo dirinfo in srcDir.GetDirectories())
        {
            CopyDirectoryAndFiles(desPath, dirinfo);
        }
    }
}